// pages/index/component/advertisement/advertisement.js
import {list} from '../../../../api/ad'
const util = require('../../../../utils/util')
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info:{
      type:Object,
    }
  },
  lifetimes:{
    attached() {
      var arr = Object.keys(this.data.info.tagids)
      if (arr.length > 0) {
        this.data.tagIds = this.data.info.tagids.split(',')
        console.log( this.data.tagIds)
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
        this.setData({
          tagIds: this.data.tagIds
        })
      }
      this.loadAd()
    }
},
  /**
   * 组件的初始数据
   */
  data: {
    adList:[],
    tagIds:[],
  
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toLink(e) {
      console.info("广告组件被点击e--"+ JSON.stringify(e))
      if(e.currentTarget.dataset.type.hasOwnProperty('typeId') && !util.isNull(e.currentTarget.dataset.type.typeId)){
        if (e.currentTarget.dataset.type.type === 0) {
          wx.navigateTo({
            url: '/pages/goods/product/details?id=' + e.currentTarget.dataset.type.typeId
          })
        } else if (e.currentTarget.dataset.type.type === 9) {
          wx.navigateTo({
            url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.type.typeId
          })
        }else if (e.currentTarget.dataset.type.type === 1) {
          wx.navigateTo({
            url: '/pages/goods/category/category?id=' + e.currentTarget.dataset.type.typeId
          })
        } else if (e.currentTarget.dataset.type.type === 2) {
          wx.navigateTo({
            url:'/pages/member/detail/detail?id='+e.currentTarget.dataset.type.typeId,
          })
        } else if (e.currentTarget.dataset.type.type === 3) {
          wx.navigateTo({
            url: '/pages/member/vip/vip'
          })
        }  
      }
    },
    loadAd(){
      list({
        tagIds: this.data.tagIds,
        mchId: app.globalData.mchId,
      }).then(res=>{
              console.info("广告组件的点击事件")
              console.info(JSON.stringify(res.data.data))
              this.data.adList = res.data.data
          this.setData({
            adList:this.data.adList
          })
      })
    }
  }
})
